package com.lightningtrade.controller;

import com.lightningtrade.domain.RecommendedCarousel;
import com.lightningtrade.log.annotaion.Log;
import com.lightningtrade.log.enums.BusinessType;
import com.lightningtrade.service.RecommendedCarouselService;
import com.lightningtrade.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.util.List;

@RestController
@RequestMapping("/carousel")
@Api(tags = "推荐商品轮播管理-0312杜晟玮")
public class RecommendedCarouselController {

    @Autowired
    private RecommendedCarouselService carouselService;


    @Log(title = "新增推荐商品", businessType = BusinessType.INSERT)
//    @PreAuthorize("hasRole('admin')")
    @ApiOperation(value = "新增推荐商品", notes = "添加一个新的推荐商品到轮播列表中")
    @PostMapping("/add")
    public AjaxResult addCarousel(@RequestBody RecommendedCarousel carousel) {
        return carouselService.addRecommendedCarousel(carousel) ?
                AjaxResult.success("新增推荐商品成功") : AjaxResult.error("新增推荐商品失败");
    }

    @ApiOperation(value = "更新推荐商品", notes = "根据ID更新推荐商品的信息")
    @PutMapping("/update")
    public AjaxResult updateCarousel(@RequestBody RecommendedCarousel carousel) {
        return carouselService.updateRecommendedCarousel(carousel) ?
                AjaxResult.success("更新推荐商品成功") : AjaxResult.error("更新推荐商品失败");
    }

    @Log(title = "删除推荐商品", businessType = BusinessType.DELETE)
//    @PreAuthorize("hasRole('admin')")
    @ApiOperation(value = "删除推荐商品", notes = "根据ID删除推荐商品")
    @DeleteMapping("/delete/{id}")
    public AjaxResult deleteCarousel(@PathVariable int id) {
        return carouselService.deleteRecommendedCarousel(id) ?
                AjaxResult.success("删除推荐商品成功") : AjaxResult.error("删除推荐商品失败");
    }

    @ApiOperation(value = "查询推荐商品", notes = "根据ID查询推荐商品详情")
    @GetMapping("/get/{id}")
    public AjaxResult getCarouselById(@PathVariable int id) {
        RecommendedCarousel carousel = carouselService.getRecommendedCarouselById(id);
        return carousel != null ? AjaxResult.success(carousel) : AjaxResult.error("未找到推荐商品");
    }


    @ApiOperation(value = "查询所有推荐商品", notes = "获取所有推荐商品的列表")
    @GetMapping("/list")
    public AjaxResult getAllCarousels() {

        return AjaxResult.success("查询成功",carouselService.getAllRecommendedCarousels());
    }
}
